// subpacks/pages_first/signIn/sign_in.js
const appg = getApp().globalData
import {
  formatTime
} from '../../../utils/date_formatter'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {
      title: null,
      currentDate: null,
      type: 0,
      sign_in_type: "人脸签到",
      classID: null
    },
    classValue: null,
    time: null,
    show: {
      show_calss: false,
      show_data: false
    },
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if (type === 'day') {
        return `${value}日`;
      }
      if (type === 'hour') {
        return `${value}小时`;
      }
      if (type === 'minute') {
        return `${value}分`;
      }
    },
    options: [], //存储班级的名字
    detailOptions: null, //记录后端返回来的数据
    loading: false
  },
  onClick(e) {
    console.log(e);
    const type = e.type;
    const optName = e.currentTarget.dataset.show
    if (optName == 'show_calss') {
      this.getGroupList()
    }
    this.setData({
      ["show." + [e.currentTarget.dataset.show]]: !this.data.show[e.currentTarget.dataset.show]
    })
  },
  onFinish(e) {
    console.log(e.detail);
    const {
      value,
      index
    } = e.detail
    const groupId = this.data.detailOptions[index].id
    this.setData({
      classValue: value,
      'form.classID': groupId,
      'show.show_calss': false
    })
  },
  onConfirm(value) {
    console.log(value);
    const time = value.detail
    const result = formatTime(time, "YYYY年MM月DD日 HH：mm")
    this.setData({
      'form.currentDate': time,
      time: result,
      'show.show_data': !this.data.show.show_data
    });
  },
  // 收集标题
  onChange(e) {
    // console.log(e.detail);
    this.setData({
      'form.title': e.detail
    })
  },
  // 获取群聊列表
  async getGroupList() {
    this.setData({
      loading: true
    })
    const uid = appg.userInfo.id
    try {
      const json = await getApp().$axios("/getGroupChatClass?id=" + uid, "GET")
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        })
        return
      }
      console.log(json);
      // 处理数据
      json.data.forEach(item => {
        this.data.options.push(item.chat_name)
      });
      this.setData({
        detailOptions: json.data,
        loading: false,
        options: this.data.options
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  // 发布
  async publish() {
    const param = {
      chat_ID: this.data.form.classID,
      uid: appg.userInfo.id,
      title: this.data.form.title,
      endTime: this.data.form.currentDate,
      chat_name: this.data.classValue,
      school_ID:appg.userInfo.school_ID
    }
    try {
      const json = await getApp().$axios("/addSign", "POST", param)
      // console.log(json);
      if (json.status != 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        })
        return
      }
      /**json.data
       * chat_id: 21
          chat_name: "测试用"
          createdAt: "2023-10-27T01:37:01.983Z"
          endTime: 1698373740000
          id: 1
          title: "测试"
          type: 0
          uid: 26
          updatedAt: "2023-10-27T01:37:01.983Z"
       */
      const id = json.data.id
      const params = {
        chat_ID: this.data.form.classID,
        uid: appg.userInfo.id,
        msg_type: 3,
        msg_content: id
      }
      const success = await getApp().$axios("/addSignMessage","POST",params)
      console.log(success);
      if (success.status != 200) {
        wx.showToast({
          title: success.data,
          icon: 'error'
        })
        return
      }
      wx.showToast({
        title: success.data,
        icon:'none'
      })
      wx.navigateBack({
        delta:1
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})